/**
 * JAVACC DEMO 1.0
 */
package com.apache.audit.server;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.client.common.ScheduledExecutor;
import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  方法执行后的日志通知
 *
 * @author Hou Dayu 创建时间：2016-8-7
 */
public class LogMethodAfterAdvice implements AfterReturningAdvice, MethodBeforeAdvice {

    private Logger log = Logger.getLogger(LogMethodAfterAdvice.class);

    /**
     * description:  组装日志对象信息
     *
     * @param userEname 操作人账号
     * @param userId 操作人ID
     * @param methodName 当前操作的方法
     * @return 返回日志对象信息
     */
    private Map<String, String> getAuditLog(Object returnValue, String userEname, String userId,
            String methodName) {
        Map<String, String> result = new HashMap<String, String>();
        if (Validator.isNotNull(userEname)) {
            result.put("userId", userId);
            result.put("userEname", userEname);
            if (methodName.startsWith("save")) {
                result.put("logType", "save");
            } else if (methodName.startsWith("edit")) {
                result.put("logType", "edit");
            } else if (methodName.startsWith("delete")) {
                result.put("logType", "del");
            }
            result.put("returnValue", String.valueOf(returnValue));
        }
        return result;
    }

    /**
     * TODO 实现方法后日志拦截.
     *
     * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
     */
    @SuppressWarnings("rawtypes")
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
            throws Throwable {
        String targetName = target.getClass().getName();
        String methodName = method.getName();
        if (targetName.indexOf("com.apache.audit") == -1) {
            if (args.length > 0) {
                try {
                    ParamsVo arguments = (ParamsVo) args[0];
                    log.info(arguments);
                    Map<String, String> map = getAuditLog(returnValue, arguments.getUserEname(),
                            arguments.getUserId(), methodName);
                    if (!Validator.isEmpty(map) && !map.isEmpty()) {
                        if (Validator.isNotNull(arguments.getInfoId())) {
                            map.put("infoId", arguments.getInfoId());
                        }
                        map.put("logClassName", arguments.getLogClassName());
                        map.put("logIp", String.valueOf(arguments.getParams("logIp")));
                        LoggerServerRunnable runnable = new LoggerServerRunnable(arguments.getObj(),
                                map);
                        ScheduledExecutor.newInstance().saveLogger(runnable);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e);
                }
            }
        }
    }

    public void before(Method method, Object[] args, Object target) throws Throwable {
        //        SystemRunnable runnable = MethodPool.getMp().getRunnable(method, args, target);
        //        if (null != runnable)
        //            ScheduledExecutor.newInstance().saveLogger(runnable);
    }

}
